package cn.est.serviceImpl;

import cn.est.dto.EvaluateDto;
import cn.est.dto.Page;
import cn.est.mapper.EvaluateMapper;
import cn.est.mapper.MalfunctionMapper;
import cn.est.mapper.MalfunctionOptionsMapper;
import cn.est.pojo.*;
import cn.est.service.EvaluateDetailService;
import cn.est.service.EvaluateService;
import cn.est.service.ModelService;
import cn.est.utils.MathUtils;
import cn.est.utils.StringUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.math.BigDecimal;
import java.util.*;

@Service
public class EvaluateServiceImpl implements EvaluateService {

    /**
     *
     */
    @Autowired
    private EvaluateMapper evaluateMapper;

    @Autowired
    private MalfunctionMapper malfunctionMapper;

    @Autowired
    private EvaluateDetailService evaluateDetailService;

    @Autowired
    private MalfunctionOptionsMapper optionsMapper;

    @Autowired
    private ModelService modelService;

    public Evaluate getEvaluateById(Long id) {
        return evaluateMapper.getEvaluateById(id);
    }

    public List<Evaluate> getEvaluateListByMap(Map<String, Object> param) {
        return evaluateMapper.getEvaluateListByMap(param);
    }

    public Integer getEvaluateCountByMap(Map<String, Object> param) {
        return evaluateMapper.getEvaluateCountByMap(param);
    }

    public Integer qdtxAddEvaluate(Evaluate evaluate) {
        evaluate.setCreatdTime(new Date());
        return evaluateMapper.insertEvaluate(evaluate);
    }

    public Integer qdtxModifyEvaluate(Evaluate evaluate) {
        evaluate.setUpdatedTime(new Date());
        return evaluateMapper.updateEvaluate(evaluate);
    }

    public Integer qdtxDeleteEvaluateById(Long id) {
        return evaluateMapper.deleteEvaluateById(id);
    }

    public Integer qdtxBatchDeleteEvaluate(String ids) {
        Map<String, List<String>> param = new HashMap<String, List<String>>();
        String[] paramArrays = ids.split(",");
        List<String> idList = new ArrayList<String>();
        for (String temp : paramArrays) {
            idList.add(temp);
        }
        param.put("ids", idList);
        return evaluateMapper.batchDeleteEvaluate(param);
    }

    public Page<Evaluate> queryEvaluatePageByMap(Map<String, Object> param, Integer pageNo, Integer pageSize) {
        Integer total = evaluateMapper.getEvaluateCountByMap(param);
        Page page = new Page(pageNo, pageSize, total);
        param.put("beginPos", page.getBeginPos());
        param.put("pageSize", page.getPageSize());
        List<Evaluate> evaluateList = evaluateMapper.getEvaluateListByMap(param);
        page.setList(evaluateList);
        return page;
    }

    @Override
    public EvaluateDto assess(Integer mid, String optionIds, Integer uid) {


        List<MalfunctionOptions> optionsList=new ArrayList<>();
        BigDecimal totalPrice =new BigDecimal(0);


        //1.查询用户选择的选项及问题的数据
        Set<Long> optionIdsSet= StringUtil.string2LongSet(optionIds,",");

        //2.单项问题维修价格查询，如果需要更换零件，则该项维修价格的比例为100%，即支付该项维修故障的最高价
        for (Long oId: optionIdsSet){
            if(oId!=null){
                MalfunctionOptions options= optionsMapper.getMalfunctionOptionsById(oId);
                Integer proccssType= options.getProcessType();  //（1:维修,2:更换配件）
                //计算该项问题的价格，入如果更换零件，则维修比例100%,需要该项故障的最高价格
                Malfunction malfunction=malfunctionMapper.getMalfunctionById(options.getMalfId());
                BigDecimal unitePrice= malfunction.getMaintainTopPrice().multiply(new BigDecimal(options.getRatio()*0.01));

                optionsList.add(options);
               totalPrice= totalPrice.add(unitePrice);
            }
        }

       //对金钱格式化，保留小数点后两位
        totalPrice= MathUtils.formatDecimal(totalPrice,2);

        //3.保存估算信息，这是保存在估算表中
        Evaluate evaluate=new Evaluate();
        evaluate.setCreatedUserId(new Long(uid));
        evaluate.setModelId(new Long(mid));
        evaluate.setPrice(totalPrice);
        evaluate.setSubscription(totalPrice.divide(new BigDecimal(10)));
        evaluate.setIsDelete(0);
         evaluateMapper.insertEvaluate(evaluate);
        //4.批量保存维修估价详情到估算详情表中   ;
        List<EvaluateDetail>  list=  evaluateDetailService.qdtxBatchAddOptions(optionsList,evaluate);
        EvaluateDto evaluateDto=new EvaluateDto();
        evaluateDto.setEvaluate(evaluate);
        evaluateDto.setDetailList(list);

        return evaluateDto;
    }

    @Override
    public EvaluateDto getEById(Integer id) {
        EvaluateDto evaluateDto=null;

        //1.根据id获取 Evaluate 对象信息
        Evaluate  evaluate =evaluateMapper.getEvaluateById(new Long(id));

        //2.根据Evaluate对象modeLiD 获取model信息
        if(evaluate!=null){
            evaluateDto=new EvaluateDto();
            Model model=modelService.getModelById(evaluate.getModelId());
            evaluateDto.setModel(model);
            evaluateDto.setEvaluate(evaluate);
        }

        //3.根据Evalute对象的id,获取所对应估算详情表数据信息
        List<EvaluateDetail>  list= evaluateDetailService.getByEId(new Long(id));


        evaluateDto.setDetailList(list);
        return evaluateDto;
    }


}
